/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.bid.service.cacheplugins  
 */
package com.apache.bid.service.plugins;

import java.util.Date;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.common.BidCommonUtil;
import com.apache.bid.common.Constant;
import com.apache.bid.common.cache.BidCacheFactory;
import com.apache.bid.common.cache.BidCacheManager;
import com.apache.bid.common.vo.BidItemCacheVo;
import com.apache.bid.entity.BidItem;
import com.apache.bid.entity.BidItemPrice;
import com.apache.bid.manager.BidItemManager;
import com.apache.bid.manager.BidItemPriceManager;
import com.apache.bid.manager.BidPubManager;
import com.apache.bid.service.cacheplugins.helper.ReductionPriceHelper;
import com.apache.cache.util.Validator;
import com.apache.tools.DateUtils;

/**
 * description:  修改标的信息
 * @author Hou Dayu 创建时间：2016-3-23  
 * 
 */
public class UnityEditItemPluginImpl implements PluginConnector {

	protected Logger log = Logger.getLogger(getClass());
	protected BidItemManager bidItemManager;
	protected BidPubManager bidPubManager;
	protected BidItemPriceManager bidItemPriceManager;

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.zbxsoft.api.manager.PluginConnector#execute(com.zbxsoft.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo param) throws Exception {
		log.info("保存标的，控制是否发布");
		BidItem item = (BidItem) param.getObj();
		ResultEntity entity = new ResultEntity();
		if (Validator.isEmpty(item)) {
			entity.setResult("false");
			entity.setMessage("缺少方法参数,方法执行失败");
		} else {
			ParamsVo<BidItem> vo = new ParamsVo<BidItem>();
			if (Validator.isNotNull(item.getBidMode()) && Validator.isNotNull(item.getFirstBidTimePre())) {
				setCloseTime(item);
			}
			BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("item");
			if (cache.checkCacheObject(BidCacheManager.item_prefix + item.getItemId())) {
				BidItemCacheVo itemVo = (BidItemCacheVo) cache.getCacheObjectByKey(BidCacheManager.item_prefix
						+ item.getItemId());
				if (Validator.isNull(item.getBidMode()))
					item.setBidMode(itemVo.getBidMode());
				if (Validator.isNotNull(item.getFirstBidTimePre())) {
					setCloseTime(item);
				}
				BidItemPrice price = (BidItemPrice) cache.getCacheCloneByKey(BidCacheManager.item_price_prefix
						+ item.getItemId());
				if (!Validator.isEmpty(price)) {//如果是已发布状态
					if (Validator.isNotNull(item.getFirstValuePre())
							&& !itemVo.getFirstValuePre().equals(item.getFirstValuePre())) {
						price.setFirstValue(item.getFirstValuePre());
					}
					if (Validator.isNotNull(item.getOpenTimePre())
							&& !itemVo.getOpenTimePre().equals(item.getOpenTimePre())) {
						price.setOpenTime(item.getOpenTimePre());
					}
					if (Validator.isNotNull(item.getFirstBidTimePre())) {
						price.setFirstBidTime(item.getFirstBidTimePre());
						price.setCloseTime(item.getCloseTimePre());
					}
					if ("T".equals(item.getvFlag()) || Constant.BIDMODE_HELAN.equals(itemVo.getBidMode())) {
						price.setReductionCloseTime(DateUtils.Now.fmt_yyyyMMdd_HHmmss());
						ReductionPriceHelper.getInstance().setParams(itemVo.getItemId(), itemVo.getItemId());
					}
					cache.updateCacheObject(cache.item_price_prefix + item.getItemId(), price);
					ParamsVo priceVo = new ParamsVo();
					priceVo.setObj(price);
					bidItemPriceManager.editInfo(priceVo);
				}
			}
			vo.setObj(item);
			bidItemManager.editInfo(vo);
			entity.setResult("true");
			entity.setMessage("规则信息保存成功");
			entity.setEntity(item.getItemId());
		}
		return entity;
	}

	private void setCloseTime(BidItem item) throws Exception {
		if (Constant.BIDMODE_JTZX.equals(item.getBidMode()) || Constant.BIDMODE_JTFX.equals(item.getBidMode())) {
			String colseTime = BidCommonUtil.getInstance().getMergeTime(item.getFirstBidTimePre(), item.getLimitTime());
			item.setCloseTimePre(colseTime);
		} else if (Constant.BIDMODE_YCXASC.equals(item.getBidMode())
				|| Constant.BIDMODE_YCXDESC.equals(item.getBidMode())) {
			item.setCloseTimePre(item.getFirstBidTimePre());
		} else if (Constant.BIDMODE_HELAN.equals(item.getBidMode())) {
			item.setFirstBidTimePre(item.getOpenTimePre());
			long limittime = Long.valueOf(item.getLimitTime()) * 1000 * 2;
			Date data = new Date(DateUtils.toLong(DateUtils.parse(item.getFirstBidTimePre(), "yyyy-MM-dd HH:mm:ss"))
					+ limittime);
			String colseTime = DateUtils.format(data, "yyyy-MM-dd HH:mm:ss");
			item.setCloseTimePre(colseTime);
		} else if (Constant.BIDMODE_FEJJZX.equals(item.getBidMode())
				|| Constant.BIDMODE_FEJJFX.equals(item.getBidMode())) {
			String colseTime = BidCommonUtil.getInstance().getMergeTime(item.getFirstBidTimePre(), item.getLimitTime());
			item.setCloseTimePre(colseTime);
		}
	}

	public void setBidItemManager(BidItemManager bidItemManager) {
		this.bidItemManager = bidItemManager;
	}

	public void setBidPubManager(BidPubManager bidPubManager) {
		this.bidPubManager = bidPubManager;
	}

	public void setBidItemPriceManager(BidItemPriceManager bidItemPriceManager) {
		this.bidItemPriceManager = bidItemPriceManager;
	}

}
